package net.narusas.aceauction.push.messageapp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import lombok.Data;

import net.narusas.aceauction.push.AuctionPushService;
import net.narusas.aceauction.push.SchedulePushService;

@Data
public class Controller {

	AuctionPushService auctionPushService;
	SchedulePushService schedulePushService;
	private AuctionPushMessageApp app;

	public void setApp(final AuctionPushMessageApp app) {
		this.app = app;
		app.sendButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				sendMessageNow(app.messageTextArea.getText());
				app.messageTextArea.setText("");
			}
		});

		app.scheduleEnableCheckBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				enableScheduler(app.scheduleEnableCheckBox.isSelected(), Integer.parseInt(app.scheduleTextField.getText()));
			}
		});

		new LogAppender(app.logTextArea);
		
		app.scheduleTextField.setText("60");

	}

	public void sendMessageNow(String message) {
		auctionPushService.pushText(message);
	}

	protected void enableScheduler(boolean enable, int min) {
		schedulePushService.setMin(min);
		schedulePushService.enable(enable);
	}

}
